using DG.Tweening;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HomeModes : MonoBehaviour
{
	[Header("Home")]
	public RectTransform buttonTransfrom;

	[Header("Popup")]
	public GameObject modePopupObject;

	public RectTransform skinContentTransform;

	public ScrollRect scrollView;

	public HomeModeSlot slotPrefab;

	private List<HomeModeSlot> _slotList = new List<HomeModeSlot>();

	private Tween _displayTween;

	private bool _canClosePopup;

	private void Awake()
	{
		modePopupObject.SetActive(value: false);
	}

	private void Start()
	{
		float width = slotPrefab.GetComponent<RectTransform>().rect.width;
		GameModeParameters[] gameModes = GameModeManager.Instance.references.gameModes;
		foreach (GameModeParameters gameMode in gameModes)
		{
			HomeModeSlot homeModeSlot = UnityEngine.Object.Instantiate(slotPrefab, slotPrefab.transform.parent);
			homeModeSlot.gameObject.SetActive(value: true);
			homeModeSlot.SetGameMode(gameMode);
			float num = (0f - width) * (float)(GameModeManager.Instance.references.gameModes.Length - 1) / 2f + (float)_slotList.Count * width;
			homeModeSlot.SetXPosition(Mathf.RoundToInt(0f - num));
			_slotList.Add(homeModeSlot);
		}
		SnapScrollView component = scrollView.GetComponent<SnapScrollView>();
		component.OnElementHighlighted.AddListener(OnModeHighlighted);
	}

	public void DisplayWithAnimation(bool state)
	{
		if (_displayTween != null)
		{
			_displayTween.Kill();
		}
		if (state)
		{
			_displayTween = buttonTransfrom.DOAnchorPosX(0f, 0.2f).SetEase(Ease.OutBack);
		}
		else
		{
			_displayTween = buttonTransfrom.DOAnchorPosX(120f, 0.2f);
		}
	}

	private void OnModeHighlighted(int index)
	{
		for (int i = 0; i < _slotList.Count; i++)
		{
			_slotList[i].SetZoom(i == index);
		}
	}

	public void CALLBACK_BUTTON_OpenGameModes()
	{
		_canClosePopup = false;
		modePopupObject.SetActive(value: true);
		for (int i = 0; i < _slotList.Count; i++)
		{
			HomeModeSlot homeModeSlot = _slotList[i];
			bool flag = homeModeSlot.CurrentModeParameters == GameModeManager.Instance.CurrentMode;
			homeModeSlot.SetSelected(flag);
			if (flag)
			{
				scrollView.content.anchoredPosition = new Vector3(homeModeSlot.XPosition, 0f);
				OnModeHighlighted(i);
			}
		}
		skinContentTransform.localScale = new Vector3(1f, 0f, 1f);
		skinContentTransform.DOScaleY(1f, 0.2f).SetDelay(0.2f).OnComplete(delegate
		{
			_canClosePopup = true;
		});
		HomeManager.Instance.DisplayHomeUI(state: false);
	}

	public void CALLBACK_BUTTON_CloseGameModes()
	{
		if (_canClosePopup)
		{
			_canClosePopup = false;
			skinContentTransform.localScale = new Vector3(1f, 1f, 1f);
			skinContentTransform.DOScaleY(0f, 0.2f).OnComplete(delegate
			{
				modePopupObject.SetActive(value: false);
				HomeManager.Instance.DisplayHomeUI(state: true);
			});
		}
	}

	public void CALLBACK_BUTTON_SelectMode(HomeModeSlot slot)
	{
		GameModeManager.Instance.CurrentMode = slot.CurrentModeParameters;
		HomeManager.Instance.OnChangeGameMode();
		for (int i = 0; i < _slotList.Count; i++)
		{
			HomeModeSlot homeModeSlot = _slotList[i];
			bool flag = homeModeSlot.CurrentModeParameters == GameModeManager.Instance.CurrentMode;
			homeModeSlot.SetSelected(flag);
			if (flag)
			{
				//scrollView.content.DOKill();
				//scrollView.content.DOAnchorPosX(homeModeSlot.XPosition, 0.2f);
				OnModeHighlighted(i);
			}
		}
	}
}
